﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Configuration;
using System.Net;
using System.IO;
using System.Text;
using System.Web.Security;
using OAuth;

public class Util
{
    protected const string OAuthParameterPrefix = "oauth_";

    public static string GetAppSettingValue(string key)
    {
        string value = "";
        value = ConfigurationManager.AppSettings.Get(key);
        return value;
    }

    public static string GetAuthenUrl()
    {
        return String.Format(GetAppSettingValue("mingAuthenUrl"), GetAppSettingValue("mingConsumerKey"), GetAppSettingValue("mingConsumerSecret"), GetAppSettingValue("site_callback"));
    }
    public static string getData(string url)
    {
        try
        {
            string result = string.Empty;

            WebRequest wrq = WebRequest.Create(url);
            WebResponse wrs = wrq.GetResponse();

            StreamReader strdr = new StreamReader(wrs.GetResponseStream(), Encoding.UTF8);
            result = strdr.ReadToEnd();
            strdr.Close();
            wrs.Close();
            return result;

        }
        catch { return ""; }
    }
    public static List<OAuth.OAuthBase.QueryParameter> GetQueryParameters(string parameters)
    {
        if (parameters.StartsWith("?"))
        {
            parameters = parameters.Remove(0, 1);
        }

        List<OAuth.OAuthBase.QueryParameter> result = new List<OAuth.OAuthBase.QueryParameter>();

        if (!string.IsNullOrEmpty(parameters))
        {
            string[] p = parameters.Split('&');
            foreach (string s in p)
            {
                if (!string.IsNullOrEmpty(s))
                {
                    if (s.IndexOf('=') > -1)
                    {
                        string[] temp = s.Split('=');
                        result.Add(new OAuth.OAuthBase.QueryParameter(temp[0], temp[1]));
                    }
                    else
                    {
                        result.Add(new OAuth.OAuthBase.QueryParameter(s, string.Empty));
                    }
                }
            }
        }

        return result;
    }
    public static string GetUserName()
    {
        if (HttpContext.Current.Request.IsAuthenticated)
        {
            FormsIdentity ident = HttpContext.Current.User.Identity as FormsIdentity;

            if (ident != null)
            {

                FormsAuthenticationTicket ticket = ident.Ticket;

                string[] userData = ticket.UserData.Split('|');

                if (userData.Length >= 3)
                {
                    return userData[0];
                }
                else
                {
                    return "Undefine";
                }

            }
            else
            {
                return "Undefine";
            }

        }
        else
        {
            return "Undefine";
        }
    }

    public static string GetEmail()
    {
        if (HttpContext.Current.Request.IsAuthenticated)
        {
            FormsIdentity ident = HttpContext.Current.User.Identity as FormsIdentity;

            if (ident != null)
            {

                FormsAuthenticationTicket ticket = ident.Ticket;

                string[] userData = ticket.UserData.Split('|');

                if (userData.Length >= 3)
                {
                    return userData[1];
                }
                else
                {
                    return "Undefine";
                }

            }
            else
            {
                return "Undefine";
            }
        }
        else
        {
            return "Undefine";
        }
    }

    public static string GetFullName()
    {
        if (HttpContext.Current.Request.IsAuthenticated)
        {
            FormsIdentity ident = HttpContext.Current.User.Identity as FormsIdentity;

            if (ident != null)
            {

                FormsAuthenticationTicket ticket = ident.Ticket;

                string[] userData = ticket.UserData.Split('|');

                if (userData.Length >= 3)
                {
                    return userData[2];
                }
                else
                {
                    return "Undefine";
                }

            }
            else
            {
                return "Undefine";
            }
        }
        else
        {
            return "Undefine";
        }
    }

}